<?php
$page = 'admin';
include '../database/connect.php';
$course_id = $_GET['course_id'];
if (isset($_GET['save'])) {
    session_start();
    extract($_POST);
    $added_by = $_SESSION['username'];
    $query = "INSERT INTO quizes SET course_id='$course_id', description='$description', due_date='$due_date',added_by='$added_by'";
    $result = mysql_query($query) or die("Error executing '$query':" . mysql_error());
    header("Location:?course_id=$course_id");
}
require_once 'header.php';

//lets get the course name for purpose of navigation
$query = "SELECT * FROM courses where id='$course_id'";
$result = mysql_query($query) or die("Error executing '$query':" . mysql_error());
$row = mysql_fetch_assoc($result);
$course = $row['course'];
?>
<h2 style="text-transform: capitalize">Course:  <?php echo $course; ?></h2>

<script type="text/javascript">
    jQuery(document).ready(function(){
        jQuery("#quiz_form").validationEngine();
    });

</script>

<p>
    <a href="admin_home.php">Admin home</a>&gt;&gt;<a href='courses.php'>Courses</a>&gt;&gt;
    <a href="lessons.php?course_id=<?php echo $course_id; ?>"><?php echo $course; ?></a>
</p>

<form action="?save=1" method="POST" id="quiz_form">
    <input type="hidden" name="course_id" value="<?php echo $_GET['course_id']; ?>" />
    <table style="width: 90%"  class="table">
        <tr><td colspan="2" style="font-weight: bold"> Add new quiz</td></tr>
        <tr><td>Quiz Description</td><td><input type="text" name="description" id="description" style="width: 59%" class="validate[required] wide"/></td></tr>
        <tr><td>Due date</td><td> <input onclick='scwShow(this,event);' style="width: 59%" class="validate[required] wide" id="due_date" name="due_date"/></td></tr>
        <tr><td></td><td><input type="submit"/></td> </tr>
    </table>
</form>

<?php
$query = "SELECT * FROM quizes where course_id='$course_id' order by date_added desc";
$result = mysql_query($query) or die("Error executing '$query':" . mysql_error());
if ($result == 0) {
    echo "No lesson in the database";
} else {
?>

    <table class="table" style="text-align: left; text-transform: capitalize">
        <tr> <th >Description</th><th > Due date</th></tr>
    <?php
    while ($row = mysql_fetch_assoc($result)) {
        extract($row);
    ?>
        <tr>
            <td ><a href="questions.php?quiz_id=<?php echo $quiz_id ?>"><?php echo $description; ?></a></td>
            <td ><?php echo $due_date; ?></td>
        </tr>
    <?php }
} ?>
</table>
<?php require_once('footer.php'); ?>